package com.ruoyi.security.service.impl;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.security.domain.AfSafetyLedger;
import com.ruoyi.security.mapper.AfSafetyLedgerMapper;
import com.ruoyi.security.service.IAfSafetyLedgerService;
import com.ruoyi.system.api.LinkFileServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 安全台账Service业务层处理
 *
 * @author Felix
 * @date 2021-10-12
 */
@Service
public class AfSafetyLedgerServiceImpl extends LinkFileServiceImpl implements IAfSafetyLedgerService {
    @Autowired
    private AfSafetyLedgerMapper afSafetyLedgerMapper;

    /**
     * 查询安全台账
     *
     * @param id 安全台账ID
     * @return 安全台账
     */
    @Override
    public AfSafetyLedger selectAfSafetyLedgerById(String id) {
        AfSafetyLedger result = afSafetyLedgerMapper.selectAfSafetyLedgerById(id);
        result.setFileList(getFileList(id.toString()));
        return result;
    }

    /**
     * 查询安全台账列表
     *
     * @param afSafetyLedger 安全台账
     * @return 安全台账
     */
    @Override
    public List<AfSafetyLedger> selectAfSafetyLedgerList(AfSafetyLedger afSafetyLedger) {
        return afSafetyLedgerMapper.selectAfSafetyLedgerList(afSafetyLedger);
    }

    /**
     * 新增安全台账
     *
     * @param afSafetyLedger 安全台账
     * @return 结果
     */
    @Override
    public int insertAfSafetyLedger(AfSafetyLedger afSafetyLedger) {
        afSafetyLedger.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
        afSafetyLedger.setCreateTime(DateUtils.getNowDate());
        afSafetyLedger.setCreateBy(SecurityUtils.getUsername());
        linkFile(afSafetyLedger.getFileIdList(), afSafetyLedger.getId());
        return afSafetyLedgerMapper.insertAfSafetyLedger(afSafetyLedger);
    }

    /**
     * 修改安全台账
     *
     * @param afSafetyLedger 安全台账
     * @return 结果
     */
    @Override
    public int updateAfSafetyLedger(AfSafetyLedger afSafetyLedger) {
        afSafetyLedger.setUpdateTime(DateUtils.getNowDate());
        afSafetyLedger.setUpdateBy(SecurityUtils.getUsername());
        linkFile(afSafetyLedger.getFileIdList(), afSafetyLedger.getId());
        return afSafetyLedgerMapper.updateAfSafetyLedger(afSafetyLedger);
    }

    /**
     * 批量删除安全台账
     *
     * @param ids 需要删除的安全台账ID
     * @return 结果
     */
    @Override
    public int deleteAfSafetyLedgerByIds(String[] ids) {
        String[] res = new String[ids.length];
        for(int i=0;i<ids.length;i++){
            res[i] = String.valueOf(ids[i]);
        }
        deleteLinkFile(res);
        return afSafetyLedgerMapper.deleteAfSafetyLedgerByIds(ids);
    }

    /**
     * 删除安全台账信息
     *
     * @param id 安全台账ID
     * @return 结果
     */
    @Override
    public int deleteAfSafetyLedgerById(String id) {
        String[] res = new String[]{id};
        deleteLinkFile(res);
        return afSafetyLedgerMapper.deleteAfSafetyLedgerById(id);
    }
}
